package com.ruiyian.project.fabric;

import com.ruiyian.common.utils.SecurityUtils;
import com.ruiyian.common.core.controller.BaseController;
import com.ruiyian.common.core.domain.AjaxResult;
import com.ruiyian.common.core.page.TableDataInfo;
import com.ruiyian.project.fabric.domain.Fabric;
import com.ruiyian.project.fabric.service.FabricService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2021/3/31
 * Desc: 画图
 */
@Slf4j
@RestController
@RequestMapping("/fabric/info")
public class FabricController extends BaseController {
    @Autowired
    private FabricService fabricService;
    /**
     * 列表
     * @param fabric
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Fabric fabric) {
        startPage();
        List<Fabric> list = fabricService.selectList(fabric);
        return getDataTable(list);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody Long id) {
        try {
            fabricService.delete(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 保存
     * @param fabric
     * @return
     */
    @PutMapping("/save")
    public AjaxResult save(@RequestBody Fabric fabric) {
        try {
            if(fabric.getId()!=null){
                fabric.setCreateBy(SecurityUtils.getUsername());
                fabricService.edit(fabric);
            }else {
                fabric.setCreateBy(SecurityUtils.getUsername());
                fabricService.save(fabric);
            }
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 编辑图例
     * @param fabric
     * @return
     */
    @PostMapping("/editFabric")
    public AjaxResult editFabric(@RequestBody Fabric fabric) {
        try {
            fabricService.editFabric(fabric);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 根据id获取对象
     * @param id
     * @return
     */
    @PostMapping("/findOne")
    public AjaxResult findOne(@RequestBody Long id) {
        Fabric fabric = null;
        try {
            fabric = fabricService.findOne(id);
        } catch (Exception e) {
            AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(fabric);
    }
}
